package cn.yunhe.service.impl;

import cn.yunhe.dao.ComboDao;
import cn.yunhe.dao.OrderDao;
import cn.yunhe.entity.Cars;
import cn.yunhe.entity.Combo;
import cn.yunhe.entity.Order;
import cn.yunhe.entity.User;
import cn.yunhe.service.OrderService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.apache.poi.hssf.usermodel.*;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.ServletOutputStream;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author  苏文秀
 * @date 2018/9/14 17:06
 */
@Service("orderService")
public class OrderServiceImpl implements OrderService {
    @Resource
    private ComboDao comboDao;
    @Resource
    private OrderDao orderDao;

    @Override
    public Page<Order> getOrderPage(Map<String, Object> map) {
        Page<Order> page=new Page<Order>();
        Integer pageNum= (Integer) map.get("pageNum");///当前页
        Integer pageSize= (Integer) map.get("pageSize");////页容量
        page.setPageSize(pageSize);
        page=PageHelper.startPage(pageNum,pageSize);
        List<Order> orderList=orderDao.getOrderList(map);
        return page;
    }

    @Override
    public boolean addInsurance(Order order) {
        return orderDao.addOrder(order);
    }

    @Override
    public Order getOrderByUserAndCars(Map<String, Object> map) {
        Order order=orderDao.getOrderByUserAndCars(map);
        return order;
    }

    @Override
    public Order getOrderDetail(String oid) {
        return orderDao.getOrderById(oid);
    }

    @Override
    public boolean updateOstate(Order order) {
        return orderDao.updateOstate(order);
    }

    @Override
    public User getUserByIdCard(String idcard) {
        return orderDao.getUserByIdCard(idcard);
    }

    @Override
    public Cars getCarsByLicensenum(String licensenum) {
        return orderDao.getCarsByLicensenum(licensenum);
    }


    @Override
    public Order getOrderInfoByCarLicenseNum(String licenseNum) {
        return orderDao.getOrderInfoByCarLicenseNum(licenseNum);
    }

    @Override
    public List<Combo> getAllCombo() {
        return comboDao.getAllCombo();
    }

    @Override
    public Order getOrderByOuid(String ouid) {
        return orderDao.getOrderByOuid(ouid);
    }

    @Override
    public boolean updateOrderPaystate(Order order) {
        return orderDao.updateOrderPaystate(order);
    }

    @Override
    public boolean updateOrderStatus(Order order) {
        return orderDao.updateOrderStatus(order);
    }

    @Override
    public void export(String[] titles, ServletOutputStream out) {
        try {
            ////创建一个workbook，对应一个Excel文件
            HSSFWorkbook workbook=new HSSFWorkbook();
            /////在webbook中加一个sheet,对应excel中的sheet
            HSSFSheet hssfSheet=workbook.createSheet("sheet1");
            ////在sheet中加头行，
            HSSFRow hssfRow=hssfSheet.createRow(0);
            /////创建单元格  并且设置格式 居中
            HSSFCellStyle hssfCellStyle=workbook.createCellStyle();
            ////设置居中样式
            hssfCellStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER);
            HSSFCell hssfCell=null;
            for (int i=0;i<titles.length;i++){
                hssfCell=hssfRow.createCell(i);
                hssfCell.setCellValue(titles[i]);
                hssfCell.setCellStyle(hssfCellStyle);
            }
            Map<String,Object> map=new HashMap<>();
            List<Order> list =orderDao.getOrderList(map);
            if (list.size()>0){
                for (int i=0;i<list.size();i++){
                    hssfRow=hssfSheet.createRow(i+1);
                    Order order=list.get(i);
                    hssfRow.createCell(0).setCellValue(order.getOid());
                    hssfRow.createCell(1).setCellValue(order.getUser().getSname());
                    hssfRow.createCell(2).setCellValue(order.getUser().getSphone());
                    hssfRow.createCell(3).setCellValue(order.getCars().getLicenseNum());
                    hssfRow.createCell(4).setCellValue(order.getAddtime());
                    String payState="";
                    if (order.getPaystate()==0){
                        payState="未缴费";
                    }else if (order.getPaystate()==1){
                        payState="已缴费";
                    }
                    hssfRow.createCell(5).setCellValue(payState);
                    String payMethod="";
                    if (order.getPaymethod()==0){
                        payMethod="未缴费";
                    }else if (order.getPaymethod()==1){
                        payMethod="支付宝";
                    }else if (order.getPaymethod()==2){
                        payMethod="微信";
                    }else if (order.getPaymethod()==3){
                        payMethod="现金";
                    }else if (order.getPaymethod()==4){
                        payMethod="银行卡";
                    }
                    hssfRow.createCell(6).setCellValue(payMethod);
                    hssfRow.createCell(7).setCellValue(order.getEmp().getEmpName());
                    hssfRow.createCell(8).setCellValue(order.getCombo().getCombname());
                    String ostate="";
                    if (order.getOstate()==0){
                        ostate="正常";
                    }else if (order.getOstate()==1){
                        ostate="退保待审核";
                    }else if (order.getOstate()==2){
                        ostate="退保成功";
                    }else if (order.getOstate()==3){
                        ostate="退保失败";
                    }else if (order.getOstate()==4){
                        ostate="未缴费";
                    }
                    hssfRow.createCell(9).setCellValue(ostate);
                    hssfRow.createCell(10).setCellValue(order.getOremark());
                }
            }

            try {
                workbook.write(out);
                out.flush();
                out.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        } catch (Exception e) {
            e.printStackTrace();
            try {
                throw new Exception("导出信息失败!请刷新重试");
            } catch (Exception e1) {
                e1.printStackTrace();
            }
        }

    }


    ////get set方法
    public OrderDao getOrderDao() {
        return orderDao;
    }

    public void setOrderDao(OrderDao orderDao) {
        this.orderDao = orderDao;
    }
}
